package com.vf.admin.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * 随机字符工具
 */
public class RandomUtil {
    private RandomUtil() {
    }
    public static RandomUtil getInstance() {
        return new RandomUtil();
    }

    public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String NUMBERCHAR = "0123456789";
    public static final String AllXCHAR = "0123456789abcdefghijklmnopqrstuvwxyz";

    /***
     * 生成uid 6位数字
     */
    public static String generateUID(){
        Random random = new Random();
        String result="";
        for(int i=0;i<6;i++){
            //首字母不能为0
            result += (random.nextInt(9)+1);
        }
        return result;
    }

    /***
     * 生成uid 8位用户code
     */
    public static String eightUserID(){
        Random random = new Random();
        String result="";
        for(int i=0;i<8;i++){
            //首字母不能为0
            result += (random.nextInt(9)+1);
        }
        return result;
    }

    /**
    * 生成uid 9位腾讯code
    * @Param: []
    * @return: java.lang.String
    * @Author: zhouyunyi
    * @Date: 2021/07/28
    * @Time: 12:50
    */
    public static String eightUserID9(){
        Random random = new Random();
        String result="";
        for(int i=0;i<9;i++){
            //首字母不能为0
            result += (random.nextInt(9)+1);
        }
        return result;
    }

    /***
     * 生成uid 7位数字
     */
    public String generateUIDYQM(){
        Random random = new Random();
        String result="";
        for(int i=0;i<10;i++){
            //首字母不能为0
            result += (random.nextInt(9)+1);
        }
        return result;
    }

    /***
     * 生成uid 10位数字
     */
    public String generateUIDYQM(Map<String, Integer> dataMap){
        if(dataMap == null){
            dataMap = new HashMap<String,Integer>();
        }
        Random random = new Random();
        String result="";
        while(result==null || result.length()<=0 || dataMap.get(result)!=null){
            result = "";
            for(int i=0;i<10;i++){
                //首字母不能为0
                result += (random.nextInt(9)+1);
            }
        }
        return result;
    }

    /***
     * 生成uid 13位数字
     */
    public String generateUIDYQMQI(){
        String result="";
        Random random = new Random();

        for(int i=0;i<7;i++){
            //首字母不能为0
            result += (random.nextInt(12)+1);
        }

        return result;
    }

    /***
     * 生成32位订单号
     */
    public static String getOrderIdByUUId(){
        int machineId = 1;//最大支持1-9个集群机器部署
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if(hashCodeV < 0) {//有可能是负数
            hashCodeV = - hashCodeV;
        }
        //0 代表前面补充0,4代表长度为4, d 代表参数为正数型
        return  machineId+String.format("%015d", hashCodeV);
    }

    /**
     * 返回一个定长的随机字符串(只包含大小写字母、数字)
     *
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public String generateString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
        }
        return sb.toString();
    }

    /**
    * 返回一个定长的随机字符串(只包含小写字母、数字)
    * @Param: [length 长度]
    * @return: java.lang.String
    * @Author: zhouyunyi
    * @Date: 2021/07/21
    */
    public String generateXxString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(AllXCHAR.charAt(random.nextInt(AllXCHAR.length())));
        }
        return sb.toString();
    }

    /**
     * 返回一个定长的随机纯字母字符串(只包含大小写字母)
     *
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public String generateMixString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length())));
        }
        return sb.toString();
    }

    /**
     * 返回一个定长的随机纯大写字母字符串(只包含大小写字母)
     *
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public String generateLowerString(int length) {
        return generateMixString(length).toLowerCase();
    }

    /**
     * 返回一个定长的随机纯小写字母字符串(只包含大小写字母)
     *
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public String generateUpperString(int length) {
        return generateMixString(length).toUpperCase();
    }

    /**
     * 生成一个定长的纯0字符串
     *
     * @param length
     *            字符串长度
     * @return 纯0字符串
     */
    public String generateZeroString(int length) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            sb.append('0');
        }
        return sb.toString();
    }

    /**
     * 根据数字生成一个定长的字符串，长度不够前面补0
     *
     * @param num
     *            数字
     * @param fixdlenth
     *            字符串长度
     * @return 定长的字符串
     */
    public String toFixdLengthString(long num, int fixdlenth) {
        StringBuffer sb = new StringBuffer();
        String strNum = String.valueOf(num);
        if (fixdlenth - strNum.length() >= 0) {
            sb.append(generateZeroString(fixdlenth - strNum.length()));
        } else {
            throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth
                    + "的字符串发生异常！");
        }
        sb.append(strNum);
        return sb.toString();
    }

    /**
     * 每次生成的len位数都不相同
     *
     * @param param
     * @return 定长的数字
     */
    public int getNotSimple(int[] param, int len) {
        Random rand = new Random();
        for (int i = param.length; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = param[index];
            param[index] = param[i - 1];
            param[i - 1] = tmp;
        }
        int result = 0;
        for (int i = 0; i < len; i++) {
            result = result * 10 + param[i];
        }
        return result;
    }

//    public static void main(String[] args){
//    	RandomUtil ranUtils = new RandomUtil();
//    	String uid = ranUtils.generateUID();
//    	System.out.println(uid);
//    }
public static String getNewFileNameuuid(){
    UUID uuid= UUID.randomUUID();
    return uuid.toString();
}

    public static void main(String[] args) {
        System.out.println("返回一个定长的随机字符串(只包含大小写字母、数字):" + RandomUtil.getInstance().generateString(50));
        System.out
                .println("返回一个定长的随机纯字母字符串(只包含大小写字母):" + RandomUtil.getInstance().generateMixString(10));
        System.out.println("返回一个定长的随机纯大写字母字符串(只包含大小写字母):"
                + RandomUtil.getInstance().generateLowerString(10));
        System.out.println("返回一个定长的随机纯小写字母字符串(只包含大小写字母):"
                + RandomUtil.getInstance().generateUpperString(10));
        System.out.println("生成一个定长的纯0字符串:" + RandomUtil.getInstance().generateZeroString(10));
        System.out.println("根据数字生成一个定长的字符串，长度不够前面补0:"
                + RandomUtil.getInstance().toFixdLengthString(123, 10));
        int[] in = { 1, 2, 3, 4, 5, 6, 7 };
        System.out.println("每次生成的len位数都不相同:" + RandomUtil.getInstance().getNotSimple(in, 3));
        System.out.println("返回一个定长的随机字符串（只包含小写字母，数字）："+RandomUtil.getInstance().generateXxString(6));
    }
}
